function [ ] = tabulate( obj, drop, filename, title, label, footnote, addmetarows )
%TABULATE Tabulate counterfactual outcomes on key variables
%   
%   INPUT:
%       obj: bidCounter object
%       drop: Drop variable from table
%       filename: Path to store table
%       title: Title of the table
%       label: Label to crossreference table
%       footnote: Table notes to describe contents
%       addmetarows: Add rows to describe variable groups
%
%

% Select variables
varIDx = logical(prod(cell2mat(cellfun(@(x) ~strcmp(x,obj.outVars), drop, 'UniformOutput',false)'),1));
outVarsTable = obj.outVars(varIDx);

% Collect table metadata
numDataCols = size(obj.cprisk,2);
numPanels = size(obj.ceiling,2);
numRowsPerPanel = size(outVarsTable,2);

% Labels
colHeaders = obj.cpriskLabs;
rowNames = obj.outVarLabs(varIDx);

% Indent row labels
for i = 1:numRowsPerPanel
    rowNames{i} = sprintf('~~~~%s',rowNames{i});
end

panelHeaders = obj.ceilingLabs;

% Formatting 
format = ['%15s',repmat('\t%4.2f',1,numDataCols)];
format = regexprep(format,'\\t','&');

label = sprintf('\\label{tab:%s}',label);

% Open file 
fid = fopen(filename,'w');

% Frontmatter
fprintf(fid, '\\begin{table}[ht]\n');
fprintf(fid, '\t\\centering\n');
fprintf(fid, '\t\t\\caption{%s%s}\n',title,label);
fprintf(fid, '\t\t\t\\begin{tabular}{%s}\n', ['l' repmat('c',1,numDataCols) ]);

% Toprule
fprintf(fid, '\t\t\t\t\\toprule\n');

% Column headers
fprintf(fid, repmat('\t',1,5));
for i = 1:numDataCols
    fprintf(fid, '&');
    fprintf(fid, '\\multicolumn{1}{c}{%s}', colHeaders{i});
    
    if i == numDataCols
        fprintf(fid, '\\\\\n');
    end
end

% Column numbers 
fprintf(fid, repmat('\t',1,5));
for i = 1:numDataCols
    fprintf(fid, '&');
    fprintf(fid, '\\multicolumn{1}{c}{(%d)}', i);
    
    if i == numDataCols
        fprintf(fid, '\\\\\n');
    end
end

% Midrule
fprintf(fid, '\t\t\t\t\t\\midrule\n');

% Iterate over panels
for i = 1:numPanels
    % Write panel header
    fprintf(fid,'\t\t\t\t\t\\addlinespace\n');
    panelName = sprintf('Panel %s: %s',char('A'+i-1),panelHeaders{i});
    fprintf(fid,'\t\t\t\t\t\\multicolumn{%i}{c}{\\emph{%s}} \\\\\n',numDataCols+1, panelName);
    fprintf(fid,'\t\t\t\t\t\\addlinespace\n');
    
    % Prepare row vector
    dataTable = vertcat(obj.outcomes{1+(i-1)*numDataCols:i*numDataCols});
    dataTable = dataTable(:,3:end);
    dataTable = dataTable(:,varIDx);
    for j = 1:numRowsPerPanel
        
        if addmetarows
            % Metadata for variable group
            if j == 1
                fprintf(fid,'\\multicolumn{1}{l}{\\emph{Participation}}\\\\\n');
            elseif j == 4
                fprintf(fid, '\\multicolumn{1}{l}{\\emph{Quantity}}\\\\\n');
            elseif j == 9
                fprintf(fid, '\\multicolumn{1}{l}{\\emph{Prices and costs}}\\\\\n');
            end
        end
        
        % Print data row
        fprintf(fid, [ '\t\t\t\t\t' format '\\\\\n' ], rowNames{j}, dataTable{:,j});
    end
end

% Endmatter
fprintf(fid, '\t\t\t\t\t\\bottomrule\n');
fprintf(fid, '\t\t\t\t\t\\multicolumn{%d}{p{%4.2f\\hsize}}{\\footnotesize %s}\n',...
    numDataCols + 1, 1.0, footnote);

fprintf(fid, '\t\t\t\\end{tabular}\n');
fprintf(fid, '\\end{table}');
fprintf(1,'Written summary statistics table to %s\n', filename);



end

